package com.game.state;

import java.util.Iterator;
import java.util.Vector;

import com.game.layer.BasicDragableLayer;
import com.game.layer.BasicLayer;

import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;


/**
 * 可以拖动的状态 继承basicState
* @author oven:
* @version 创建时间：2012-11-5 下午2:26:29
* 
*/
public abstract class BasicDragSacleAbleState extends BasicState {
	
	protected boolean holdingTransCanvas=false;
	protected float canvasHoldX;
	protected float canvasHoldY;
	protected float canvasTransX;
	protected float canvasTransY;
	protected float basicScaleValue;
	protected float currentScaleValue = 1;
	protected Vector<BasicDragableLayer> dragAbleLayers= new Vector<BasicDragableLayer>();
	
	
	/**
	 * 判断计算拖动 及放大缩小
	 * @param event
	 * @return
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		//点击屏幕的坐标
		float postScreenX = event.getX();
		float postScreenY = event.getY();
		//点击点相对canvas的坐标
		float postCanvasX = postScreenX-canvasTransX;
		float postCanvasY = postScreenY-canvasTransY;
		//重新封装触摸的事件
//		event.setLocation(postCanvasX,postCanvasY);
		int action=event.getAction();
		//子方法中触发了触摸事件则不移动
		boolean childReceiveFlag = super.onTouchEvent(event);
		if(!childReceiveFlag){
			if (event.getPointerCount() == 2) {
	            float x = event.getX(0) - event.getX(1);
	            float y = event.getY(0) - event.getY(1);
	            float value = (float) Math.sqrt(x * x + y * y);// 计算两点的距离
	            if (basicScaleValue == 0) {
	                basicScaleValue = value;
	            } 
	            else {
	                if (value - basicScaleValue >= 10 || value - basicScaleValue <= -10) {
	                    currentScaleValue = value / basicScaleValue;// 当前两点间的距离除以手指落下时两点间的距离就是需要缩放的比例。
	                    //缩放图层
	                    Iterator<BasicDragableLayer> dragLayers = dragAbleLayers.iterator();
						while(dragLayers.hasNext()){
							dragLayers.next().updateDragInfo(value);
						}
	                }
	            }
	        } 
	        else if (event.getPointerCount() == 1) {
	        	basicScaleValue=0;
	        	gd.onTouchEvent(event);
//	        	switch(action){
//					//按下
//					case MotionEvent.ACTION_DOWN:{
//						//标识按下
//						holdingTransCanvas=true;
//						canvasHoldX=postScreenX;
//						canvasHoldY=postScreenY;
//						break;
//					}
//					case MotionEvent.ACTION_MOVE:{
//						Log.i("infos","正在移动");
//						canvasTransX=canvasTransX+postScreenX-canvasHoldX;
//						canvasTransY=canvasTransY+postScreenY-canvasHoldY;
//						canvasHoldX=postScreenX;
//						canvasHoldY=postScreenY;
//						break;
//					}
//					//提起
//					case MotionEvent.ACTION_UP:{
//						Log.i("infos", "ACTION_UP"+action);
//						holdingTransCanvas=false;
//						canvasHoldX=canvasTransX;
//						canvasHoldY=canvasTransX;
//						break;	
//					}
//	        	}
	        }
			
		}
		return true;
	}
	
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
		//调用父类的onScrool
		boolean childReceiveFlag=super.onScroll(e1, e2, distanceX, distanceY);
		if(!childReceiveFlag){
			Log.i("infos","正在拖动");
			canvasTransX=canvasTransX-distanceX;
			canvasTransY=canvasTransY-distanceY;
			Iterator<BasicDragableLayer> dragLayers = dragAbleLayers.iterator();
			while(dragLayers.hasNext()){
				dragLayers.next().updateDragInfo(distanceX,distanceY);
			}
		}
		return false;
	}
	
	
	/**
	 * 删除图层
	 */
	@Override
	public void removeLayer(BasicLayer layer) {
		super.removeLayer(layer);
		if(layer instanceof BasicDragableLayer){
			unRegisterDragAbleLayer((BasicDragableLayer)layer);
		}
	}
	
	/**
	 * 添加并显示图层
	 */
	@Override
	public void addAndShowLayer(BasicLayer layer) {
		addLayer(layer);
		layer.show();
	}
	
	/**
	 * 添加图层
	 */
	@Override
	public void addLayer(BasicLayer layer) {
		super.addLayer(layer);
		if(layer instanceof BasicDragableLayer){
			registerDragableLayer((BasicDragableLayer)layer);
		}
	}
	
	/**
	 * 注册可拖动图层
	 * @param layer
	 */
	public void registerDragableLayer(BasicDragableLayer layer){
		if(!dragAbleLayers.contains(layer)){
			dragAbleLayers.add(layer);
		}
	}
	
	/**
	 * 注销可拖动图层
	 * @param layer
	 */
	public void unRegisterDragAbleLayer(BasicDragableLayer layer){
		dragAbleLayers.remove(layer);
	}
	
	public boolean isHoldingTransCanvas() {
		return holdingTransCanvas;
	}
	public void setHoldingTransCanvas(boolean holdingTransCanvas) {
		this.holdingTransCanvas = holdingTransCanvas;
	}
	public float getCanvasHoldX() {
		return canvasHoldX;
	}
	public void setCanvasHoldX(float canvasHoldX) {
		this.canvasHoldX = canvasHoldX;
	}
	public float getCanvasHoldY() {
		return canvasHoldY;
	}
	public void setCanvasHoldY(float canvasHoldY) {
		this.canvasHoldY = canvasHoldY;
	}
	public float getCanvasTransX() {
		return canvasTransX;
	}
	public void setCanvasTransX(float canvasTransX) {
		this.canvasTransX = canvasTransX;
	}
	public float getCanvasTransY() {
		return canvasTransY;
	}
	public void setCanvasTransY(float canvasTransY) {
		this.canvasTransY = canvasTransY;
	}
	public float getBaseValue() {
		return basicScaleValue;
	}
	public void setBaseValue(float baseValue) {
		this.basicScaleValue = baseValue;
	}
	public float getmCurrentScale() {
		return currentScaleValue;
	}
	public void setmCurrentScale(float mCurrentScale) {
		this.currentScaleValue = mCurrentScale;
	}


}

